package o2o.web.actions.company;

import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ObjectUtils;
import com.jinyou.utils.common.ValidateUtil;
import com.jinyouapp.common.tools.jms.model.JMSNotify;
import o2o.cache.company.SysCityCompanyMemory;
import o2o.cache.company.SysCityCompanyUserRelationMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.config.dao.UserDaoSql;
import o2o.config.jms.config.JMSTypeContent;
import o2o.config.jms.sender.JmsSender;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.common.UserType;
import o2o.dao.PO.company.SYS_COMPANY_USER_TYPE;
import o2o.dao.PO.company.SysCityCompanyInfo;
import o2o.dao.PO.company.SysCityCompanyUser;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.authrole.RoleInfoVO;
import o2o.dao.VO.company.SysCityCompanyUserVO;
import o2o.system.data.SYS_CONFIG_PATH;
import o2o.web.actions.authrole.AuthRoleMan;
import org.apache.commons.lang.StringUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @User: @Created by yangtk
 * @Date: @Date 2019/7/15 16:26
 * @Classname: SysCityCompanyMan
 * @To change this template use File | Settings | File Templates.
 */
public class SysCityCompanyMan {

    // ************************************公司相关********************

    // 获取公司信息
    public synchronized static SysCityCompanyInfo getByCompanyUsername(String username) {
        if (StringUtils.isEmpty(username)) {
            return null;
        }
        Long companyId = SysCityCompanyUserRelationMemory.getInstance().getCompanyIdByUsername(username);
        if (companyId == null) {
            UserInfo userInfo = UserInfoMemory.getInstance().getUserInfo(username);
            if (userInfo != null && (userInfo.getUserType() == UserType.SUPER_ADMIN && userInfo.getUserType() != UserType.ADMIN)) {
                SysCityCompanyInfo companyInfo = new SysCityCompanyInfo();
                Long time = DateUtils.getCurrTime();
                try {
                    Connection con = UserDaoSql.getInstance().getConnection();
                    companyInfo.setAddress("").setProvince(userInfo.getProvince())
                            .setCity(userInfo.getCity()).setCounty(userInfo.getCity()).setCompanyName("");
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
                SysCityCompanyUserRelationMemory.getInstance().add(time);
                SysCityCompanyMemory.getInstance().add(time);
            }
        }
        SysCityCompanyInfo info = SysCityCompanyMemory.getInstance().getById(companyId);
        return info;
    }

    // 获取公司的员工信息
    public static List<SysCityCompanyUser> listOfCompanyUserByCompanyId(Long companyId) throws Exception {
        if (companyId == null) {
            throw new Exception("公司id不能为空");
        }
        List<SysCityCompanyUser> companyUsers = SysCityCompanyUserRelationMemory.getInstance().listOfByCompanyId(companyId);
        return companyUsers;
    }

    // 获取公司员工信息
    public static List<SysCityCompanyUserVO> listOfCompanyUserVOByCompanyId(Long companyId, String username) throws Exception {
        if (companyId == null) {
            throw new Exception("公司id不能为空");
        }
        List<SysCityCompanyUser> companyUsers = listOfCompanyUserByCompanyId(companyId);
        if (companyUsers == null) {
            return null;
        }
        List<SysCityCompanyUserVO> result = new ArrayList<>();
        for (SysCityCompanyUser companyUser : companyUsers) {
            if (companyUser == null || StringUtils.isEmpty(companyUser.getUsername())) {
                continue;
            }
            if ("admin".equalsIgnoreCase(companyUser.getUsername()) || "sadmin".equalsIgnoreCase(companyUser.getUsername())) {
                continue;
            }

            UserInfo info = UserInfoMemory.getInstance().getUserInfo(companyUser.getUsername());
            SysCityCompanyUserVO cityCompanyUserVO = new SysCityCompanyUserVO();
            try {
                ObjectUtils.copyPropertiesExclude(companyUser, cityCompanyUserVO);
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
            if (info != null) {
                if (null != info.getUserType() && 0 == UserType.SUPER_ADMIN - info.getUserType()) {
                    continue;
                }
                try {
                    ObjectUtils.copyPropertiesExclude(info, cityCompanyUserVO);
                } catch (Exception e) {
                    e.printStackTrace();
                    continue;
                }
            }
            if (!ValidateUtil.isNull(cityCompanyUserVO.getSignPhoto()) && ValidateUtil.isNotNetUrl(cityCompanyUserVO.getSignPhoto())) {
                cityCompanyUserVO.setSignPhoto(SYS_CONFIG_PATH.IMAGE_BASE_URL + cityCompanyUserVO.getSignPhoto());
            }
            // 角色
            List<RoleInfoVO> roleInfoVOS = AuthRoleMan.listOfRoleInfoVOByUsername(cityCompanyUserVO.getUsername());
            if (roleInfoVOS != null) {
                cityCompanyUserVO.setRoleInfos(roleInfoVOS);
            }
            if (ValidateUtil.isNotNull(username)) {
                if ((ValidateUtil.isNotNull(cityCompanyUserVO.getUsername()) && cityCompanyUserVO.getUsername().contains(username))
                        || (ValidateUtil.isNotNull(cityCompanyUserVO.getPhone()) && cityCompanyUserVO.getPhone().contains(username))
                        || (ValidateUtil.isNotNull(cityCompanyUserVO.getTelPhone()) && cityCompanyUserVO.getTelPhone().contains(username))
                        ) {
                    result.add(cityCompanyUserVO);
                } else {
                    continue;
                }
            } else {
                result.add(cityCompanyUserVO);
            }
        }
        return result;
    }

    // 修改员工信息
    public static void updateSysCityCompanyUser(String username, int companyUserType, String updateUser) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" update ").append(PojoTables.SYS_CITY_COMPANY_USER)
                .append(" set updateUser ='").append(updateUser).append("'")
                .append(",type=").append(companyUserType).append(" where delFlag = 0 and username='").append(username).append("'");

        UserDaoSql.getInstance().executeBySql(sql.toString());
        SysCityCompanyUserRelationMemory.getInstance().modify(username);
        // 数据通讯
        JMSNotify notify = JMSNotify.getInstance().setType(JMSTypeContent.SYS_CITY_COMPANY_USER_UPDATE).setStr01(username);
        JmsSender.setTopicSender(notify);
    }

    // 账户

    /**
     * 用户是不是超级管理所在公司的账户
     *
     * @param username
     * @return
     */
    public static boolean isSuperAdminCompanyUser(String username) {
        UserInfo userInfo = UserInfoMemory.getInstance().getUserInfo(username);
        if (userInfo != null && userInfo.getUserType() == UserType.SUPER_ADMIN) {
            return true;
        }
        SysCityCompanyInfo companyInfo = SysCityCompanyMan.getByCompanyUsername(username);
        if (companyInfo == null) {
            return false;
        }
        return isSuperAdminCompany(companyInfo.getId());
    }

    /**
     * 用户是不是代理所在公司的账户
     *
     * @param username
     * @return
     */
    public static boolean isAdminCompanyUser(String username) {
        UserInfo userInfo = UserInfoMemory.getInstance().getUserInfo(username);
        if (userInfo != null && userInfo.getUserType() == UserType.ADMIN) {
            return true;
        }
        SysCityCompanyInfo companyInfo = SysCityCompanyMan.getByCompanyUsername(username);
        if (companyInfo == null) {
            return false;
        }
        List<SysCityCompanyUser> companyUsers = SysCityCompanyUserRelationMemory.getInstance().listOfByCompanyId(companyInfo.getId());
        if (companyUsers != null) {
            for (SysCityCompanyUser sys : companyUsers) {
                if (sys == null) {
                    continue;
                }
                UserInfo info = UserInfoMemory.getInstance().getUserInfo(sys.getUsername());
                if (info == null) {
                    continue;
                }
                if (info.getUserType() == UserType.ADMIN) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 根据账号获取用户所在公司的拥有者
     *
     * @param username
     * @return
     */
    public static String getSysCompanyOwnUsername(String username) {
        UserInfo info = UserInfoMemory.getInstance().getUserInfo(username);
        if (info != null && info.getUserType() == UserType.ADMIN) {
            return username;
        } else if (info != null && info.getUserType() == UserType.COMPANY) {
            SysCityCompanyInfo companyInfo = SysCityCompanyMan.getByCompanyUsername(username);
            if (companyInfo == null) {
                return null;
            }
            List<SysCityCompanyUser> companyUsers = SysCityCompanyUserRelationMemory.getInstance().listOfByCompanyId(companyInfo.getId());
            if (companyUsers != null) {
                for (SysCityCompanyUser sys : companyUsers) {
                    if (sys == null) {
                        continue;
                    }
                    if (sys.getType() == SYS_COMPANY_USER_TYPE.OWN) {
                        return sys.getUsername();
                    }
                }
            }
            return null;

        } else {
            return username;
        }
    }

    // 是不是超级管理员所在的公司
    public static boolean isSuperAdminCompany(Long companyId) {
        List<SysCityCompanyUser> companyUsers = SysCityCompanyUserRelationMemory.getInstance().listOfByCompanyId(companyId);
        if (companyUsers != null) {
            for (SysCityCompanyUser sys : companyUsers) {
                if (sys == null) {
                    continue;
                }
                UserInfo info = UserInfoMemory.getInstance().getUserInfo(sys.getUsername());
                if (info == null) {
                    continue;
                }
                if (info.getUserType() == UserType.SUPER_ADMIN) {
                    return true;
                }
            }
        }
        return false;
    }


}
